﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


using Microsoft.Phone.Controls;
using System.Xml.Linq;
using Microsoft.Phone.Shell;


namespace MolfMolfMolf
{
    public partial class NewsPage : PhoneApplicationPage
    {
        List<NewsItem> newses = new List<NewsItem>();
        List<NewsItem> leftNews = new List<NewsItem>();
        List<NewsItem> rightNews = new List<NewsItem>();
        WebClient client = new WebClient();

        public NewsPage()
        {
            InitializeComponent();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

            client.DownloadStringAsync(new Uri("http://www.golf.fi/uutinen/-/journal/rss/41255?doAsGroupId=10162&refererPlid=16171&_15_groupId=10162"));
            
        }
        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            leftNews.Clear();
            rightNews.Clear();

            try
            {
                XDocument x = XDocument.Parse(e.Result);

                var news = from n in x.Descendants("item") select n;
                foreach (var n in news)
                {
                    string[] split = new string[1];
                    split[0] = " - ";
                    string[] results = (n.Element("title").Value.Split(split, StringSplitOptions.RemoveEmptyEntries));
                    string title = results[results.Length - 1];
                    string des = n.Element("description").Value;
                    string[] split1 = new string[1];
                    split1[0] = "GMT";
                    string[] results_date = (n.Element("pubDate").Value.Split(split1, StringSplitOptions.RemoveEmptyEntries));
                    string date = results_date[0];
                    string link = n.Element("link").Value;
                    NewsItem item = new NewsItem(title, des, date, link);
                    item.Tap += new EventHandler<GestureEventArgs>(item_Tap);
                    newses.Add(item);
                }
            }
            catch
            {
                MessageBox.Show("News feed is not accessible now!");
            }

            int count = 0;
            foreach (NewsItem n in newses)
            {
                if (count % 2 == 0)
                {
                    leftNews.Add(n);
                }
                else
                {
                    rightNews.Add(n);
                }
                count++;
            }
            LeftBox.DataContext = leftNews;
            RightBox.DataContext = rightNews;
        }

        void item_Tap(object sender, GestureEventArgs e)
        {
            NewsItem item = (NewsItem)sender;
            PhoneApplicationService.Current.State["uri"] = item.uri;
            PhoneApplicationService.Current.State["imgUri"] = item.imgUri;
            PhoneApplicationService.Current.State["title"] = item.newstitle;
            PhoneApplicationService.Current.State["date"] = item.date;
            NavigationService.Navigate(new Uri("/NewsDetailPage.xaml", UriKind.Relative));
        }
    }
}